package cn.lili.modules.wallet.aop.interceptor;

import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.utils.CurrencyUtil;
import cn.lili.modules.payment.entity.dos.UserFPointsHistory;
import cn.lili.modules.payment.entity.dos.UserPointsHistory;
import cn.lili.modules.payment.entity.dos.UserWallet;
import cn.lili.modules.payment.entity.dto.UserFmoneyUpdateDTO;
import cn.lili.modules.payment.entity.dto.UserPointUpdateDTO;
import cn.lili.modules.wallet.service.UserFPointsHistoryService;
import cn.lili.modules.wallet.service.UserPointsHistoryService;
import cn.lili.modules.wallet.service.WalletService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * F分操作切面
 *
 * @author Chopper
 * @since 2020/11/17 7:22 下午
 */
@Slf4j
@Aspect
@Component
@RequiredArgsConstructor
public class FMoneyLogInterceptor {

    private final UserFPointsHistoryService userFPointsHistoryService;

    private final WalletService walletService;

    @After("@annotation(cn.lili.modules.wallet.aop.annotation.FMoneyLogPoint)")
    public void doAfter(JoinPoint pjp) {
        //参数
        Object[] obj = pjp.getArgs();
        try {
            //变动积分
            if (obj[0] != null) {
                UserFmoneyUpdateDTO userFmoneyUpdateDTO = (UserFmoneyUpdateDTO) obj[0];
                Double point = userFmoneyUpdateDTO.getFmoney();
                //变动类型
                Boolean isAdd = true;
                if (userFmoneyUpdateDTO.getUserFmoneyServiceEnum() != null) {
                    isAdd = userFmoneyUpdateDTO.getUserFmoneyServiceEnum().getAdd();
                }
                // 会员ID
                String memberId = "";
                if (CharSequenceUtil.isNotEmpty(userFmoneyUpdateDTO.getUserId())) {
                    memberId = userFmoneyUpdateDTO.getUserId();
                }
                // 变动积分为0，则直接返回
                if (point == 0) {
                    return;
                }

                //根据会员id查询会员信息
                UserWallet userWallet = walletService.getFromUserid(memberId);
                UserFPointsHistory userFPointsHistory = new UserFPointsHistory();
                userFPointsHistory.setMemberId(userWallet.getUserId());
                userFPointsHistory.setMemberName(userWallet.getNickName());
                userFPointsHistory.setPointType(userFmoneyUpdateDTO.getUserFmoneyServiceEnum().name());

                userFPointsHistory.setVariablePoint(point);
                if (Boolean.TRUE.equals(isAdd)) {
                    userFPointsHistory.setBeforePoint(CurrencyUtil.sub(userWallet.getFmoney(), point));
                } else {
                    userFPointsHistory.setBeforePoint(CurrencyUtil.add(userWallet.getFmoney(), point));
                }
                userFPointsHistory.setPoints(userWallet.getFmoney());
                userFPointsHistory.setContent(userFmoneyUpdateDTO.getDescription());
                userFPointsHistory.setCreateBy("系统");
                userFPointsHistoryService.save(userFPointsHistory);
            }

        } catch (Exception e) {
            log.error("F分操作错误", e);
        }
    }

}
